package cn.zh.service.impl;

import cn.hutool.crypto.digest.MD5;
import cn.zh.commons.ResponseResult;
import cn.zh.entity.Admin;
import cn.zh.entity.User;
import cn.zh.form.LoginForm;
import cn.zh.mapper.AdminMapper;
import cn.zh.service.AdminService;
import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Resource
    private AdminMapper adminMapper;


    public ResponseResult getPersonalInfo(String username) {

       Admin admin= adminMapper.selectByUsername(username);
        return ResponseResult.success().data(admin);
    }


    public ResponseResult editPersonalInfo(Admin admin) {
        adminMapper.editPersonalInfo(admin);
        return ResponseResult.success().message("修改成功");
    }

    public ResponseResult editPersonalPassword(String oldPassword, String password, HttpServletRequest request) {

        HttpSession session = request.getSession();
        Admin admin=(Admin) session.getAttribute("admin");
        //判断当前密码是否正确
        String username = admin.getUsername();
        String DBPassword = adminMapper.selectByUsername(username).getPassword();
        String formPassword = MD5.create().digestHex16(oldPassword);
        if (!StringUtils.equals(DBPassword,formPassword)){
            return ResponseResult.fail().message("原始密码不正确");
        }
        // 2.修改密码
        String newPassword = MD5.create().digestHex16(password);
        adminMapper.editPersonalPassword(newPassword,admin.getId());

        //清空session
        session.invalidate();

        return ResponseResult.success().message("密码修改成功");
    }



    public ResponseResult adminPage(Integer page, Integer limit, String username) {
        PageHelper.startPage(page,limit);
        List<Admin> admins = this.adminMapper.searchAdmins(username);
        PageInfo<Admin> adminPageInfo = new PageInfo<Admin>(admins);
        long total = adminPageInfo.getTotal();
        return ResponseResult.success().data(admins).count(total);
    }

    public ResponseResult addAdmin(Admin admin) {
        String defaultPassword = "123456";
        String Md5Password = MD5.create().digestHex16(defaultPassword);
        admin.setPassword(Md5Password);
        Admin admin1 = this.adminMapper.selectByAdminName(admin.getUsername());
        if (!ObjectUtils.isEmpty(admin1)) {
            return ResponseResult.fail().message("管理员已存在！");
        } else {
            this.adminMapper.insertAdmin(admin);
            return ResponseResult.success().message("添加成功！");
        }
    }

    public ResponseResult removeAdmin(List<Integer> ids) {
        this.adminMapper.deleteAdmin(ids);
        return ResponseResult.success().message("删除成功！");
    }

    public ResponseResult resetPassword(Integer id) {
        String password = "111111";
        String md5Password = MD5.create().digestHex16(password);
        this.adminMapper.updatePassword(id, md5Password);
        return ResponseResult.success().message("密码重置成功！");
    }

    public ResponseResult getByAdminId(Integer id) {
        Admin admin = this.adminMapper.selectById(id);
        return ResponseResult.success().data(admin);
    }

    public ResponseResult editAdmin(Admin admin) {
        this.adminMapper.updateAdmin(admin);
        return ResponseResult.success().message("编辑成功！");
    }

    public ResponseResult login(String username1, String password1, String captcha, HttpServletRequest request) {

        LoginForm loginForm = new LoginForm()
                .setUsername(username1)
                .setPassword(password1)
                .setCaptcha(captcha);

        //验证码
        String captcha1 = loginForm.getCaptcha();
        HttpSession session = request.getSession();
        String code = (String) session.getAttribute("code");

        if (!StringUtils.equals(captcha1,code)){
            return ResponseResult.fail().message("验证码错误");
        }

        //用户名
        Admin admin = adminMapper.selectByAdminName(loginForm.getUsername());
        if (ObjectUtils.isEmpty(admin)){
            return ResponseResult.fail().message("用户名或密码错误");
        }

        //密码
        String MD5Password = MD5.create().digestHex16(loginForm.getPassword());
        String DBPassword = admin.getPassword();
        if (!StringUtils.equals(MD5Password,DBPassword)){
            return ResponseResult.fail().message("用户名或密码错误");
        }

        //将用户信息缓存在session中，用户后期非法请求拦截业务
        session.setAttribute("admin",admin);

        return ResponseResult.success().message("认证通过").data(admin);
    }
}
